<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'controllers/admin.php';

/**
 * Контроллер панели админа
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class Admin_Dashboard extends Admin {
    
    /**
     * News feed
     * @var string
     */
    const HWD_NEWS_FEED = 'http://hwdteam.ru/category/news/feed';
    
    /**
     * News count
     * @var int
     */
    const HWD_NEWS_COUNT = 5;
     
    /**
     * Конструктор класса
     *
     * @access public
     * @return Admin_Dashboard
     */
    function Admin_Dashboard() {
        parent::Admin();
        $this->set_menu(1, 1);
        $this->load->library('license');
        $this->load->library('cache');
        $this->load->helper('http');
    } // end Admin_Dashboard
     
    /**
     * Метод по умолчанию
     *
     * @access public
     */
    function index() {
        $data = array();
        
        // License
        if ($this->license->isActivated()) {
            $data['show_license'] = array(array('license' => $this->variables->get('license')));
            $data['not_show_license'] = array();
        } else {
            $data['show_license'] = array();
            $data['not_show_license'] = array(array());
        }
        
        // News
        $news = array();
        if ($this->cache->is_cached('hwdteam_news')) {
            $news = $this->cache->get('hwdteam_news');
        } else {
            $content = get_content(self::HWD_NEWS_FEED, array(), 5);
            if (!empty($content)) {
                $xml = simplexml_load_string($content);
                if ($xml) {
                    $i = self::HWD_NEWS_COUNT;
                    foreach ($xml->channel->item as $item) {
                        array_push($news, array(
                            'title' => (string) $item->title,
                            'link' => (string) $item->link,
                            'description' => (string) $item->description,
                        ));
                        if (--$i == 0) {
                            break;
                        }
                    }
                }
            }
            $this->cache->save('hwdteam_news', $news);
        }
        $data['news_items'] = $news;
        
        $this->set_content($this->parser->parse('admin/dashboard.html', $data, true));
        $this->display();
    } // end index
     
    function activation() {
        if ($this->license->isActivated()) {
            $this->set_message($this->lang->line('already_activated'), 'help', true);
            $this->redirect($this->get_controller());
        }
        $data = array();
        // Стандартные поля
        $license = $this->variables->get('license');
        $email = $this->variables->get('site_email');
        // Валидируем и, если нужно, изменяем
        if (false !== $this->input->post('edit')) {
            $this->load->library('validation');
            $rules = array(
            	'license' => 'trim|required',
                'email' => 'trim|required|valid_email|max_length[255]',
            );
            $this->validation->set_rules($rules);
            $result = $this->validation->run();
            // Получаем из поста поля
            $license = $this->validation->license;
            $email = $this->validation->email;
            if ($result) {
                if ($this->license->activate($license, $email)) {
                    $this->set_message($this->lang->line('activation_succ'), 'info', true);
                    $this->redirect($this->get_controller());
                } else {
                    $error = $this->license->getLastError();
                    $this->set_message($this->lang->line(empty($error) ? 'activation_error' : 'LICENSE_ERROR_' . $error), 'error');
                }
            }
        }
        // Забиваем в дату поля
        $data['email'] = htmlspecialchars($email);
        if (!($data['license'] = htmlspecialchars($license))) {
            if (isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST']) {
                $data['license'] = $_SERVER['HTTP_HOST'];
            } elseif (isset($_SERVER['SERVER_NAME']) && $_SERVER['SERVER_NAME']) {
                $data['license'] = $_SERVER['SERVER_NAME'];
            } else {
                $data['license'] = $_SERVER['REMOTE_ADDR'];
            }
        }
        
        // Выводим
        $this->set_content($this->parser->parse('admin/activation.html', $data, true));
        $this->display();
    }
     
}
